home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_qt.idb / usr / freeware / include / qcanvas.h.z / qcanvas.h
C/C++ Source or Header  |  2001-04-12  |  17KB  |  678 lines

  1. /**********************************************************************
  2. ** $Id: qt/src/canvas/qcanvas.h   2.3.0   edited 2001-01-26 $
  3. **
  4. ** Definition of QCanvas classes
  5. **
  6. ** Created : 991211
  7. **
  8. ** Copyright (C) 1999-2000 Trolltech AS.  All rights reserved.
  9. **
  10. ** This file is part of the canvas module of the Qt GUI Toolkit.
  11. **
  12. ** This file may be distributed under the terms of the Q Public License
  13. ** as defined by Trolltech AS of Norway and appearing in the file
  14. ** LICENSE.QPL included in the packaging of this file.
  15. **
  16. ** This file may be distributed and/or modified under the terms of the
  17. ** GNU General Public License version 2 as published by the Free Software
  18. ** Foundation and appearing in the file LICENSE.GPL included in the
  19. ** packaging of this file.
  20. **
  21. ** Licensees holding valid Qt Enterprise Edition licenses may use this
  22. ** file in accordance with the Qt Commercial License Agreement provided
  23. ** with the Software.
  24. **
  25. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  26. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  27. **
  28. ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
  29. **   information about Qt Commercial License Agreements.
  30. ** See http://www.trolltech.com/qpl/ for QPL licensing information.
  31. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  32. **
  33. ** Contact info@trolltech.com if any conditions of this licensing are
  34. ** not clear to you.
  35. **
  36. **********************************************************************/
  37.  
  38. #ifndef QCANVAS_H
  39. #define QCANVAS_H
  40.  
  41. #ifndef QT_H
  42. #include <qbitmap.h>
  43. #include <qwidget.h>
  44. #include <qscrollview.h>
  45. #include <qlist.h>
  46. #include <qptrdict.h>
  47. #endif // QT_H
  48.  
  49. #ifndef QT_NO_CANVAS
  50.  
  51.  
  52. class QCanvasSprite;
  53. class QCanvasPolygonalItem;
  54. class QCanvasRectangle;
  55. class QCanvasPolygon;
  56. class QCanvasEllipse;
  57. class QCanvasText;
  58. class QCanvasLine;
  59. class QCanvasChunk;
  60. class QCanvas;
  61. class QCanvasData;
  62. class QCanvasItem;
  63. class QCanvasView;
  64. class QCanvasPixmap;
  65.  
  66.  
  67. #if defined(Q_TEMPLATEDLL)
  68. // MOC_SKIP_BEGIN
  69. template class Q_EXPORT QList< QCanvasItem >;
  70. template class Q_EXPORT QList< QCanvasView >;
  71. template class Q_EXPORT QValueList< QCanvasItem* >;
  72. // MOC_SKIP_END
  73. #endif
  74.  
  75.  
  76. class QCanvasItemList : public QValueList<QCanvasItem*> {
  77. public:
  78.     void sort();
  79.     void drawUnique( QPainter& painter );
  80. };
  81.  
  82.  
  83. class QCanvasItemExtra;
  84.  
  85. class Q_EXPORT QCanvasItem : public Qt
  86. {
  87. public:
  88.     QCanvasItem(QCanvas* canvas);
  89.     virtual ~QCanvasItem();
  90.  
  91.     double x() const
  92.     { return myx; }
  93.     double y() const
  94.     { return myy; }
  95.     double z() const
  96.     { return myz; } // (depth)
  97.  
  98.     virtual void moveBy(double dx, double dy);
  99.     void move(double x, double y);
  100.     void setX(double a) { move(a,y()); }
  101.     void setY(double a) { move(x(),a); }
  102.     void setZ(double a) { myz=a; changeChunks(); }
  103.  
  104.     bool animated() const;
  105.     virtual void setAnimated(bool y);
  106.     virtual void setVelocity( double vx, double vy);
  107.     void setXVelocity( double vx ) { setVelocity(vx,yVelocity()); }
  108.     void setYVelocity( double vy ) { setVelocity(xVelocity(),vy); }
  109.     double xVelocity() const;
  110.     double yVelocity() const;
  111.     virtual void advance(int stage);
  112.  
  113.     virtual bool collidesWith( const QCanvasItem* ) const=0;
  114.  
  115.     QCanvasItemList collisions(bool exact /* NO DEFAULT */ ) const;
  116.  
  117.     virtual void setCanvas(QCanvas*);
  118.  
  119.     virtual void draw(QPainter&)=0;
  120.  
  121.     void show();
  122.     void hide();
  123.  
  124.     virtual void setVisible(bool yes);
  125.     bool visible() const
  126.     { return (bool)vis; }
  127.     virtual void setSelected(bool yes);
  128.     bool selected() const
  129.     { return (bool)sel; }
  130.     virtual void setEnabled(bool yes);
  131.     bool enabled() const
  132.     { return (bool)ena; }
  133.     virtual void setActive(bool yes);
  134.     bool active() const
  135.     { return (bool)act; }
  136.  
  137.     virtual int rtti() const;
  138.  
  139.     virtual QRect boundingRect() const=0;
  140.     virtual QRect boundingRectAdvanced() const;
  141.  
  142.     QCanvas* canvas() const
  143.     { return cnv; }
  144.  
  145. private:
  146.     // For friendly sublasses...
  147.  
  148.     friend class QCanvasPolygonalItem;
  149.     friend class QCanvasSprite;
  150.     friend class QCanvasRectangle;
  151.     friend class QCanvasPolygon;
  152.     friend class QCanvasEllipse;
  153.     friend class QCanvasText;
  154.     friend class QCanvasLine;
  155.  
  156.     virtual QPointArray chunks() const;
  157.     virtual void addToChunks();
  158.     virtual void removeFromChunks();
  159.     virtual void changeChunks();
  160.     virtual bool collidesWith(   const QCanvasSprite*,
  161.                  const QCanvasPolygonalItem*,
  162.                  const QCanvasRectangle*,
  163.                  const QCanvasEllipse*,
  164.                  const QCanvasText* ) const=0;
  165.     // End of friend stuff
  166.  
  167.     QCanvas* cnv;
  168.     static QCanvas* current_canvas;
  169.     double myx,myy,myz;
  170.     QCanvasItemExtra *ext;
  171.     QCanvasItemExtra& extra();
  172.     uint ani:1;
  173.     uint vis:1;
  174.     uint sel:1;
  175.     uint ena:1;
  176.     uint act:1;
  177. };
  178.  
  179.  
  180. class Q_EXPORT QCanvas : public QObject
  181. {
  182.     Q_OBJECT
  183. public:
  184.     QCanvas( QObject* parent = 0, const char* name = 0 );
  185.     QCanvas(int w, int h);
  186.     QCanvas( QPixmap p, int h, int v, int tilewidth, int tileheight );
  187.  
  188.     virtual ~QCanvas();
  189.  
  190.     virtual void setTiles( QPixmap tiles, int h, int v,
  191.                int tilewidth, int tileheight );
  192.     virtual void setBackgroundPixmap( const QPixmap& p );
  193.     QPixmap backgroundPixmap() const;
  194.  
  195.     virtual void setBackgroundColor( const QColor& c );
  196.     QColor backgroundColor() const;
  197.  
  198.     virtual void setTile( int x, int y, int tilenum );
  199.     int tile( int x, int y ) const
  200.     { return grid[x+y*htiles]; }
  201.  
  202.     int tilesHorizontally() const
  203.     { return htiles; }
  204.     int tilesVertically() const
  205.     { return vtiles; }
  206.  
  207.     int tileWidth() const
  208.     { return tilew; }
  209.     int tileHeight() const
  210.     { return tileh; }
  211.  
  212.     virtual void resize(int width, int height);
  213.     int width() const
  214.     { return awidth; }
  215.     int height() const
  216.     { return aheight; }
  217.     QSize size() const
  218.     { return QSize(awidth,aheight); }
  219.     bool onCanvas( int x, int y ) const
  220.     { return x>=0 && y>=0 && x<awidth && y<aheight; }
  221.     bool onCanvas( const QPoint& p ) const
  222.     { return onCanvas(p.x(),p.y()); }
  223.     bool validChunk( int x, int y ) const
  224.     { return x>=0 && y>=0 && x<chwidth && y<chheight; }
  225.     bool validChunk( const QPoint& p ) const
  226.     { return validChunk(p.x(),p.y()); }
  227.  
  228.     int chunkSize() const
  229.     { return chunksize; }
  230.     virtual void retune(int chunksize, int maxclusters=100);
  231.  
  232.     bool sameChunk(int x1, int y1, int x2, int y2) const
  233.     { return x1/chunksize==x2/chunksize && y1/chunksize==y2/chunksize; }
  234.     virtual void setChangedChunk(int i, int j);
  235.     virtual void setChangedChunkContaining(int x, int y);
  236.     virtual void setAllChanged();
  237.     virtual void setChanged(const QRect& inarea);
  238.  
  239.     // These call setChangedChunk.
  240.     void addItemToChunk(QCanvasItem*, int i, int j);
  241.     void removeItemFromChunk(QCanvasItem*, int i, int j);
  242.     void addItemToChunkContaining(QCanvasItem*, int x, int y);
  243.     void removeItemFromChunkContaining(QCanvasItem*, int x, int y);
  244.  
  245.     QCanvasItemList allItems();
  246.     QCanvasItemList collisions( const QPoint&) const;
  247.     QCanvasItemList collisions( const QRect&) const;
  248.     QCanvasItemList collisions( const QPointArray& pa, const QCanvasItem* item,
  249.                 bool exact) const;
  250.  
  251.     // These are for QCanvasView to call
  252.     virtual void addView(QCanvasView*);
  253.     virtual void removeView(QCanvasView*);
  254.     void drawArea(const QRect&, QPainter* p=0, bool double_buffer=TRUE);
  255.  
  256.     // These are for QCanvasItem to call
  257.     virtual void addItem(QCanvasItem*);
  258.     virtual void addAnimation(QCanvasItem*);
  259.     virtual void removeItem(QCanvasItem*);
  260.     virtual void removeAnimation(QCanvasItem*);
  261.  
  262.     virtual void setAdvancePeriod(int ms);
  263.     virtual void setUpdatePeriod(int ms);
  264.  
  265.     virtual void setDoubleBuffering(bool y);
  266.  
  267. signals:
  268.     void resized();
  269.  
  270. public slots:
  271.     virtual void advance();
  272.     virtual void update();
  273.  
  274. protected:
  275.     virtual void drawBackground(QPainter&, const QRect& area);
  276.     virtual void drawForeground(QPainter&, const QRect& area);
  277.  
  278. private:
  279.     void init(int w, int h, int chunksze=16, int maxclust=100);
  280.  
  281.     QCanvasChunk& chunk(int i, int j) const;
  282.     QCanvasChunk& chunkContaining(int x, int y) const;
  283.  
  284.     void drawChanges(const QRect& inarea);
  285.  
  286.     QPixmap offscr;
  287.     int awidth,aheight;
  288.     int chunksize;
  289.     int maxclusters;
  290.     int chwidth,chheight;
  291.     QCanvasChunk* chunks;
  292.  
  293.     QCanvasData* d;
  294.  
  295.     void initTiles(QPixmap p, int h, int v, int tilewidth, int tileheight);
  296.     ushort *grid;
  297.     ushort htiles;
  298.     ushort vtiles;
  299.     ushort tilew;
  300.     ushort tileh;
  301.     bool oneone;
  302.     QPixmap pm;
  303.     QTimer* update_timer;
  304.     QColor bgcolor;
  305.     bool debug_redraw_areas;
  306.     bool dblbuf;
  307.  
  308.     friend void qt_unview(QCanvas* c);
  309. };
  310.  
  311. class Q_EXPORT QCanvasView : public QScrollView
  312. {
  313.     Q_OBJECT
  314. public:
  315.     QCanvasView(QCanvas* viewing=0, QWidget* parent=0, const char* name=0, WFlags f=0);
  316.     ~QCanvasView();
  317.  
  318.     QCanvas* canvas() const
  319.     { return viewing; }
  320.     void setCanvas(QCanvas* v);
  321.  
  322. protected:
  323.     void drawContents( QPainter*, int cx, int cy, int cw, int ch );
  324.     QSize sizeHint() const;
  325.  
  326. private:
  327.     QCanvas* viewing;
  328.     friend void qt_unview(QCanvas* c);
  329.  
  330. private slots:
  331.     void cMoving(int,int);
  332.     void updateContentsSize();
  333. };
  334.  
  335.  
  336. class Q_EXPORT QCanvasPixmap : public QPixmap
  337. {
  338. public:
  339.     QCanvasPixmap(const QString& datafilename);
  340.     QCanvasPixmap(const QImage& image);
  341.     QCanvasPixmap(const QPixmap&, QPoint hotspot);
  342.     ~QCanvasPixmap();
  343.  
  344.     int offsetX() const
  345.     { return hotx; }
  346.     int offsetY() const
  347.     { return hoty; }
  348.     void setOffset(int x, int y) { hotx = x; hoty = y; }
  349.  
  350. private:
  351.     void init(const QImage&);
  352.     void init(const QPixmap& pixmap, int hx, int hy);
  353.  
  354.     friend class QCanvasSprite;
  355.     friend class QCanvasPixmapArray;
  356.     friend bool qt_testCollision(const QCanvasSprite* s1, const QCanvasSprite* s2);
  357.  
  358.     int hotx,hoty;
  359.  
  360.     QImage* collision_mask;
  361. };
  362.  
  363.  
  364. class Q_EXPORT QCanvasPixmapArray
  365. {
  366. public:
  367.     QCanvasPixmapArray();
  368.     QCanvasPixmapArray(const QString& datafilenamepattern, int framecount=0);
  369.     QCanvasPixmapArray(QList<QPixmap>, QList<QPoint> hotspots);
  370.     ~QCanvasPixmapArray();
  371.  
  372.     bool readPixmaps(const QString& datafilenamepattern, int framecount=0);
  373.     bool readCollisionMasks(const QString& filenamepattern);
  374.  
  375.     int operator!(); // Failure check.
  376.  
  377.     QCanvasPixmap* image(int i) const
  378.     { return img[i]; }
  379.     void setImage(int i, QCanvasPixmap* p);
  380.     uint count() const
  381.     { return (uint)framecount; }
  382.  
  383. private:
  384.     bool readPixmaps(const QString& datafilenamepattern, int framecount, bool maskonly);
  385.  
  386.     void reset();
  387.     int framecount;
  388.     QCanvasPixmap** img;
  389. };
  390.  
  391.  
  392. class Q_EXPORT QCanvasSprite : public QCanvasItem
  393. {
  394. public:
  395.     QCanvasSprite(QCanvasPixmapArray* array, QCanvas* canvas);
  396.  
  397.     void setSequence(QCanvasPixmapArray* seq);
  398.  
  399.     virtual ~QCanvasSprite();
  400.  
  401.     void move(double x, double y);
  402.     virtual void move(double x, double y, int frame);
  403.     void setFrame(int);
  404.     int frame() const
  405.     { return frm; }
  406.     int frameCount() const
  407.     { return images->count(); }
  408.  
  409.     virtual int rtti() const;
  410.  
  411.     bool collidesWith( const QCanvasItem* ) const;
  412.  
  413.     QRect boundingRect() const;
  414.  
  415. protected:
  416.     void draw(QPainter& painter);
  417.  
  418.     int width() const;
  419.     int height() const;
  420.  
  421.     int absX() const;
  422.     int absY() const;
  423.     int absX2() const;
  424.     int absY2() const;
  425.  
  426.     int absX(int nx) const;
  427.     int absY(int ny) const;
  428.     int absX2(int nx) const;
  429.     int absY2(int ny) const;
  430.     QCanvasPixmap* image() const
  431.     { return images->image(frm); }
  432.     virtual QCanvasPixmap* imageAdvanced() const;
  433.     QCanvasPixmap* image(int f) const
  434.     { return images->image(f); }
  435.  
  436. private:
  437.     void addToChunks();
  438.     void removeFromChunks();
  439.     void changeChunks();
  440.  
  441.     int frm;
  442.     bool collidesWith( const QCanvasSprite*,
  443.                const QCanvasPolygonalItem*,
  444.                const QCanvasRectangle*,
  445.                const QCanvasEllipse*,
  446.                const QCanvasText* ) const;
  447.  
  448.     friend bool qt_testCollision( const QCanvasSprite* s1, 
  449.                   const QCanvasSprite* s2 );
  450.  
  451.     QCanvasPixmapArray* images;
  452. };
  453.  
  454. class QPolygonalProcessor;
  455.  
  456. class Q_EXPORT QCanvasPolygonalItem : public QCanvasItem
  457. {
  458. public:
  459.     QCanvasPolygonalItem(QCanvas* canvas);
  460.     virtual ~QCanvasPolygonalItem();
  461.  
  462.     bool collidesWith( const QCanvasItem* ) const;
  463.  
  464.     virtual void setPen(QPen p);
  465.     virtual void setBrush(QBrush b);
  466.  
  467.     QPen pen() const
  468.     { return pn; }
  469.     QBrush brush() const
  470.     { return br; }
  471.  
  472.     virtual QPointArray areaPoints() const=0;
  473.     virtual QPointArray areaPointsAdvanced() const;
  474.     QRect boundingRect() const;
  475.  
  476.     int rtti() const;
  477.  
  478. protected:
  479.     void draw(QPainter &);
  480.     virtual void drawShape(QPainter &) = 0;
  481.  
  482.     bool winding() const;
  483.     void setWinding(bool);
  484.  
  485. private:
  486.     void scanPolygon( const QPointArray& pa, int winding,
  487.               QPolygonalProcessor& process ) const;
  488.     QPointArray chunks() const;
  489.  
  490.     bool collidesWith( const QCanvasSprite*,
  491.                const QCanvasPolygonalItem*,
  492.                const QCanvasRectangle*,
  493.                const QCanvasEllipse*,
  494.                const QCanvasText* ) const;
  495.  
  496.     QBrush br;
  497.     QPen pn;
  498.     uint wind:1;
  499. };
  500.  
  501.  
  502. class Q_EXPORT QCanvasRectangle : public QCanvasPolygonalItem
  503. {
  504. public:
  505.     QCanvasRectangle(QCanvas* canvas);
  506.     QCanvasRectangle(const QRect&, QCanvas* canvas);
  507.     QCanvasRectangle(int x, int y, int width, int height, QCanvas* canvas);
  508.  
  509.     ~QCanvasRectangle();
  510.  
  511.     int width() const;
  512.     int height() const;
  513.     void setSize(int w, int h);
  514.     QSize size() const
  515.     { return QSize(w,h); }
  516.     QPointArray areaPoints() const;
  517.     QRect rect() const
  518.     { return QRect(int(x()),int(y()),w,h); }
  519.  
  520.     bool collidesWith( const QCanvasItem* ) const;
  521.  
  522.     int rtti() const;
  523.  
  524. protected:
  525.     void drawShape(QPainter &);
  526.     QPointArray chunks() const;
  527.  
  528. private:
  529.     bool collidesWith(   const QCanvasSprite*,
  530.              const QCanvasPolygonalItem*,
  531.              const QCanvasRectangle*,
  532.              const QCanvasEllipse*,
  533.              const QCanvasText* ) const;
  534.  
  535.     int w, h;
  536. };
  537.  
  538.  
  539. class Q_EXPORT QCanvasPolygon : public QCanvasPolygonalItem
  540. {
  541. public:
  542.     QCanvasPolygon(QCanvas* canvas);
  543.     ~QCanvasPolygon();
  544.     void setPoints(QPointArray);
  545.     QPointArray points() const;
  546.     void moveBy(double dx, double dy);
  547.  
  548.     QPointArray areaPoints() const;
  549.     int rtti() const;
  550.  
  551. protected:
  552.     void drawShape(QPainter &);
  553.     QPointArray poly;
  554. };
  555.  
  556.  
  557. class Q_EXPORT QCanvasLine : public QCanvasPolygonalItem
  558. {
  559. public:
  560.     QCanvasLine(QCanvas* canvas);
  561.     ~QCanvasLine();
  562.     void setPoints(int x1, int y1, int x2, int y2);
  563.  
  564.     QPoint startPoint() const
  565.     { return QPoint(x1,y1); }
  566.     QPoint endPoint() const
  567.     { return QPoint(x2,y2); }
  568.  
  569.     int rtti() const;
  570.  
  571.     void setPen(QPen p);
  572.  
  573. protected:
  574.     void drawShape(QPainter &);
  575.     QPointArray areaPoints() const;
  576.  
  577. private:
  578.     int x1,y1,x2,y2;
  579. };
  580.  
  581.  
  582. class Q_EXPORT QCanvasEllipse : public QCanvasPolygonalItem
  583. {
  584.  
  585. public:
  586.     QCanvasEllipse( QCanvas* canvas );
  587.     QCanvasEllipse( int width, int height, QCanvas* canvas );
  588.     QCanvasEllipse( int width, int height, int startangle, int angle,
  589.             QCanvas* canvas );
  590.  
  591.     ~QCanvasEllipse();
  592.  
  593.     int width() const;
  594.     int height() const;
  595.     void setSize(int w, int h);
  596.     void setAngles(int start, int length);
  597.     int angleStart() const
  598.     { return a1; }
  599.     int angleLength() const
  600.     { return a2; }
  601.     QPointArray areaPoints() const;
  602.  
  603.     bool collidesWith( const QCanvasItem* ) const;
  604.  
  605.     int rtti() const;
  606.  
  607. protected:
  608.     void drawShape(QPainter &);
  609.  
  610. private:
  611.     bool collidesWith( const QCanvasSprite*,
  612.                const QCanvasPolygonalItem*,
  613.                const QCanvasRectangle*,
  614.                const QCanvasEllipse*,
  615.                const QCanvasText* ) const;
  616.     int w, h;
  617.     int a1, a2;
  618. };
  619.  
  620.  
  621. class QCanvasTextExtra;
  622.  
  623. class Q_EXPORT QCanvasText : public QCanvasItem
  624. {
  625. public:
  626.     QCanvasText(QCanvas* canvas);
  627.     QCanvasText(const QString&, QCanvas* canvas);
  628.     QCanvasText(const QString&, QFont, QCanvas* canvas);
  629.  
  630.     virtual ~QCanvasText();
  631.  
  632.     void setText( const QString& );
  633.     void setFont( const QFont& );
  634.     void setColor( const QColor& );
  635.     QString text() const;
  636.     QFont font() const;
  637.     QColor color() const;
  638.  
  639.     void moveBy(double dx, double dy);
  640.  
  641.     int textFlags() const
  642.     { return flags; }
  643.     void setTextFlags(int);
  644.  
  645.     QRect boundingRect() const;
  646.  
  647.     bool collidesWith( const QCanvasItem* ) const;
  648.  
  649.     virtual int rtti() const;
  650.  
  651. protected:
  652.     virtual void draw(QPainter&);
  653.  
  654. private:
  655.     void addToChunks();
  656.     void removeFromChunks();
  657.     void changeChunks();
  658.  
  659.     void setRect();
  660.     QRect brect;
  661.     QString txt;
  662.     int flags;
  663.     QFont fnt;
  664.     QColor col;
  665.     QCanvasTextExtra* extra;
  666.  
  667.     bool collidesWith(   const QCanvasSprite*,
  668.              const QCanvasPolygonalItem*,
  669.              const QCanvasRectangle*,
  670.              const QCanvasEllipse*,
  671.              const QCanvasText* ) const;
  672. };
  673.  
  674.  
  675. #endif // QT_NO_CANVAS
  676.  
  677. #endif // QCANVAS_H
  678.